package jdk8的特性.使用流来收集数据;

import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.*;

/**
 * @author idea
 * @data 2018/9/16
 */
public class Test {

    public static void main(String[] args) {
        List<Transaction> transactionList = MockTransaction.mockTransactionList();
        if (transactionList != null) {
            Comparator<Transaction> transactionComparator = Comparator.comparingInt(Transaction::getTotalPrice);
            //获取交易单据中金额最大的
            Optional<Transaction> mostPriceTransaction = transactionList.stream()
                    .collect(maxBy(transactionComparator));
            Transaction mostTransaction = mostPriceTransaction.get();
            System.out.println(mostTransaction.getTotalPrice());

            //获取交易单据中金额最小的
            Optional<Transaction> leastPriceTransaction = transactionList.stream()
                    .collect(minBy(transactionComparator));
            Transaction leastTransaction = leastPriceTransaction.get();
            System.out.println(leastTransaction.getTotalPrice());

            //获取交易单据中的金额平均值
            double avg=transactionList.stream()
                    .collect(averagingInt(Transaction::getTotalPrice));
            System.out.println("交易平均金额"+avg);

            //交易总额(这里面包含有总数，总量，平均数，最大最小值，功能强大)
            IntSummaryStatistics summaryStatistics=transactionList.stream()
                    .collect(summarizingInt(Transaction::getTotalPrice));
            System.out.println(summaryStatistics.toString());

        }
    }
}
